/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.action.form;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import nhadat.common.CommonConstant;
import nhadat.entity.TblTinDang;
import nhadat.dao.TindangDao;
import nhadat.tindang.DientichkhuonvienType;
import nhadat.tindang.DientichxaydungType;
import nhadat.tindang.GiatienType;
import nhadat.tindang.KhuvucType;
import nhadat.tindang.TindangType;
import nhadat.tindangfull.ChieudaikhuonvienType;
import nhadat.tindangfull.ChieudaixaydungType;
import nhadat.tindangfull.ChieungangkhuonvienType;
import nhadat.tindangfull.ChieungangxaydungType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.TindangfullType;
import nhadat.tindangfull.DientichkhuonvienfullType;
import nhadat.tindangfull.DientichxaydungfullType;
import nhadat.tindangfull.GiatienfullType;
import nhadat.tindangfull.KhuvucfullType;
import nhadat.tindangfull.TindangfullType;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;
import nhadat.xml.StaxParser;
import nhadat.xml.XMLReportLogic;
import nhadat.xml.XMLmanipulate;
import nhadat.xml.objecttype.report.TinduocxemType;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Xe Bay
 */
public class ChiTietNhaDatActionForm extends org.apache.struts.action.ActionForm {

    private String msts;

    public String getMsts() {
        return msts;
    }

    public void setMsts(String msts) {
        this.msts = msts;
    }

    public TblTinDang xemChiTiet() {
        TindangDao tindang = new TindangDao();
        StaxParser reader = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");

        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_DATABASE).toString());
        if (Integer.parseInt(msts) <= tmp) {
            TblTinDang[] result = tindang.getAllTindangByID(msts);

            if (result != null) {
                return result[0];
            }
        } else {
            List<TindangfullType> result = reader.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            for (int i = 0; i< result.size(); i++) {
                if (msts.equals(result.get(i).getMatindang())) {
                    String loainhaFullXML = path + "xml/TblLoaiNha.xml";
                    String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
                    String khuvucFullXML = path + "xml/TblKhuVuc.xml";
                    String loaitinFullXML = path + "xml/TblLoaiTin.xml";

                    DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
                    TblTinDang tin = syn.mapTblTinDangWithTindangFullType(result.get(i), loainhaFullXML, loaigiaytoFullXML, khuvucFullXML, loaitinFullXML);

                    return tin;
                }
            }
        }


        return null;
    }

    public void updateSoLuotXem() {
        UpdateToXMLFull();
        UpdateToXML();
    }

    public void UpdateToXMLFull() {
        TindangfullType[] dsTindang;
        TindangType[] dsTindang2;
        String path = getServlet().getServletContext().getRealPath("/");
        BigInteger soluotxemOld = new BigInteger("0");
        StaxParser reader = new StaxParser();
        TblTinDang updatedTin = new TblTinDang();
        TindangDao tindang = new TindangDao();
        int tmp = Integer.parseInt(getServlet().getServletContext().getAttribute(CommonConstant.MAX_MATINDANG_DATABASE).toString());
        if (Integer.parseInt(msts) <= tmp) {
            TblTinDang[] result = tindang.getAllTindangByID(msts);
            if (result != null) {
                updatedTin = result[0];
            }
        } else {
            List<TindangfullType> result = reader.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
            for (int i = 0; i< result.size(); i++) {
                if (msts.equals(result.get(i).getMatindang())) {
                    String loainhaFullXML = path + "xml/TblLoaiNha.xml";
                    String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
                    String khuvucFullXML = path + "xml/TblKhuVuc.xml";
                    String loaitinFullXML = path + "xml/TblLoaiTin.xml";

                    DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
                    updatedTin  = syn.mapTblTinDangWithTindangFullType(result.get(i), loainhaFullXML, loaigiaytoFullXML, khuvucFullXML, loaitinFullXML);


                }
            }
        }
        DientichkhuonvienfullType dientichkhuonvien = new DientichkhuonvienfullType();
        ChieungangkhuonvienType chieungangkhuonvien = new ChieungangkhuonvienType();
        ChieudaikhuonvienType chieudaikhuonvien = new ChieudaikhuonvienType();

        DientichxaydungfullType dientichxaydung = new DientichxaydungfullType();
        ChieungangxaydungType chieungangxaydung = new ChieungangxaydungType();
        ChieudaixaydungType chieudaixaydung = new ChieudaixaydungType();

        KhuvucfullType khuvuc = new KhuvucfullType();
        GiatienfullType giatien = new GiatienfullType();

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        List<TindangfullType> listTinDangFullType = parser.readXMLtoTindangFullTypebyStAX(path + "xml/TindangFull.xml");
        dsTindang = new TindangfullType[listTinDangFullType.size() + 1];
        for (int i = 0; i < listTinDangFullType.size(); i++) {
            dsTindang[i] = listTinDangFullType.get(i);
        }
        List<TindangType> listTindangType = parser.readXMLtoTindangTypebyStAX(path + "xml/Tindang.xml");
        dsTindang2 = new TindangType[listTindangType.size()];
        for (int i = 0; i < listTindangType.size(); i++) {
            dsTindang2[i] = listTindangType.get(i);
        }
        for (int i = 0; i < dsTindang2.length; i++) {
            if (Integer.parseInt(dsTindang2[i].getMatindang()) == updatedTin.getMatindang()) {
                soluotxemOld = dsTindang2[i].getSoluotxem();
            }
        }

        dientichkhuonvien.setDvddientichkhuonvien(updatedTin.getDvddientichkhuonvien());
        dientichkhuonvien.setSolieu(updatedTin.getDientichkhuonvien());
        chieungangkhuonvien.setDvdchieungangkhuonvien(updatedTin.getDvdchieungangkhuonvien());
        chieungangkhuonvien.setSolieu(updatedTin.getChieungangkhuonvien());
        chieudaikhuonvien.setDvdchieudaikhuonvien(updatedTin.getDvdchieudaikhuonvien());
        chieudaikhuonvien.setSolieu(updatedTin.getChieudaikhuonvien());


        dientichxaydung.setDvddientichxaydung(updatedTin.getDvddientichxaydung());
        dientichxaydung.setSolieu(updatedTin.getDientichxaydung());
        chieungangxaydung.setDvdchieungangxaydung(updatedTin.getDvdchieungangxaydung());
        chieungangxaydung.setSolieu(updatedTin.getChieungangxaydung());
        chieudaixaydung.setDvdchieudaixaydung(updatedTin.getDvdchieudaixaydung());
        chieudaixaydung.setSolieu(updatedTin.getChieudaixaydung());

        khuvuc.setQuanhuyen(updatedTin.getTblKhuVuc().getQuanhuyen());
        khuvuc.setTinhthanh(updatedTin.getTblKhuVuc().getTinhthanh());

        giatien.setSolieu(updatedTin.getGiatien());
        giatien.setDvdgiatien(updatedTin.getDvdgiatien());

        
        TindangfullType newTin = new TindangfullType();

        newTin.setMatindang(updatedTin.getMatindang() + "");
        newTin.setTenloaitin(updatedTin.getTblLoaiTin().getTenloaitin());
        newTin.setTenloainha(updatedTin.getTblLoaiNha().getTenloainha());
        newTin.setTenloaigiayto(updatedTin.getTblLoaiGiayTo().getTenloaigiayto());
        newTin.setThongtinchitiet(updatedTin.getThongtinchitiet());
        newTin.setCautruc(updatedTin.getCautruc());
        newTin.setDientichkhuonvien(dientichkhuonvien);
        newTin.setChieungangkhuonvien(chieungangkhuonvien);
        newTin.setChieudaikhuonvien(chieudaikhuonvien);
        newTin.setDientichxaydung(dientichxaydung);
        newTin.setChieungangxaydung(chieungangxaydung);
        newTin.setChieudaixaydung(chieudaixaydung);
        newTin.setDiachi(updatedTin.getDiachi());
        newTin.setKhuvuc(khuvuc);
        newTin.setHinhanh(updatedTin.getHinhanh());
        newTin.setTieudetin(updatedTin.getTieudetin());
        newTin.setGiatien(giatien);
        newTin.setNguoidangtin(updatedTin.getTblNguoiDung().getTendangnhap());
        newTin.setNgaydangtin(updatedTin.getNgaydangtin() + "");
        
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.s");
        String dateNow = formatter.format(currentDate.getTime());
        
        newTin.setLancuoicapnhat(dateNow);
        newTin.setSoluotxem( soluotxemOld.add(new BigInteger("1")) );
        newTin.setStatus("update");
        newTin.setIsActive(updatedTin.getIsActived() + "");

        dsTindang[listTinDangFullType.size()] = newTin;

        test.createXMLfromTindangFullType(dsTindang, path + "xml/TindangFull.xml");



    }

    public void UpdateToXML() {

        XMLmanipulate test = new XMLmanipulate();
        StaxParser parser = new StaxParser();
        String path = getServlet().getServletContext().getRealPath("/");
        List<TindangType> result = parser.readXMLtoTindangTypebyStAX(path + "xml/Tindang.xml");
        TindangType[] dsTindang = new TindangType[result.size()];
        for (int i = 0; i < result.size(); i++) {
            dsTindang[i] = result.get(i);
        }
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.s");
        String dateNow = formatter.format(currentDate.getTime());
        for (int i = 0; i < dsTindang.length; i++) {
            if (dsTindang[i].getMatindang().equals(msts)) {

                dsTindang[i].setLancuoicapnhat(dateNow);
                dsTindang[i].setSoluotxem(dsTindang[i].getSoluotxem().add(new BigInteger("1")));
                
                dsTindang[i].setStatus("update");

            }
        }

        test.createXMLfromTindangType(dsTindang, path + "xml/Tindang.xml");

    }

    public void recordForReport(){
        TblTinDang tblTin = xemChiTiet();
        TinduocxemType tin = new TinduocxemType();
        tin.setMatindang(msts);
        tin.setLoaitin(tblTin.getTblLoaiTin().getTenloaitin());
        tin.setSoluotxem("1");

        XMLReportLogic xmlrl = new XMLReportLogic();
        xmlrl.setContextPath(getServlet().getServletContext().getRealPath("/"));
        xmlrl.recordTinDuocXem(tin, CommonConstant.ACTION_VIEW);
    }

    public ChiTietNhaDatActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }
}
